# author = '李天宝'
# write in '2020/8/5 0005 11:00'

from rest_framework.views import APIView
from rest_framework.response import Response
from common.moexception import NoDataException
from LoginSign.models import User,address

#这个页面是为了添加收件地址用的
class AddAddress(APIView):
    """
    post
    username 收件人姓名
    telephone 收件人电话
    province_name 收件人 省
    city_name 收件人 市
    region_name 是否是默认地址
    flag (可以不填) 随便填点东西就是表示是默认地址
    """
    def post(self,request):
        if 'username' not in request.data:
            raise NoDataException("没有填写收件人姓名")
        if 'telephone' not in request.data:
            raise NoDataException("没有填写收件人手机号")
        if 'province_name' not in request.data:
            raise NoDataException("没有填写收件人所在的省")
        if 'city_name' not in request.data:
            raise NoDataException("没有填写收件人所在市")
        if 'region_name' not in request.data:
            raise NoDataException("没有填写所在的详细地址")

        user=User.objects.first()
        data=request.data
        addr=address(user_id=user,user_name=data['username'])
        addr.province_name=data['province_name']
        addr.city_name=data['city_name']
        addr.user_phone=data['telephone']
        addr.region_name=data['region_name']
        if 'flag' in data['flag']:
            addr.default_flag=True
        addr.default_flag=False
        addr.save()

        return Response({
            'code':200,
            'detail':"地址注册成功"
        })